package org.telegram.bot.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.telegram.bot.domain.BotGroupJoin;
import org.telegram.bot.domain.BotJob;
import org.telegram.bot.domain.BotKeyboard;
import org.telegram.bot.handle.IBotSendMessage;
import org.telegram.bot.service.IBotGroupJoinService;
import org.telegram.bot.service.IBotJobService;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("botTask")
@Slf4j
public class BotTask implements IBotSendMessage
{

    @Resource
    private IBotJobService botJobService;
    
    @Resource
    private IBotGroupJoinService botGroupJoinService;
    public void execute(Long botJobId)
    {
        BotJob botJob = botJobService.selectBotJobById(botJobId);
        if (botJob == null)
        {
            return;
        }
        BotKeyboard keyboard = new BotKeyboard(botJob.getConfig());
        List<BotGroupJoin> botGroupJoins = botGroupJoinService.selectBotGroupJoinChat(botJob.getChatId());
        botGroupJoins.forEach(botInfo -> {
            send(botInfo.getBotId(), SendMessage.builder().chatId(botJob.getChatId()).text(keyboard.getContent()).replyMarkup(keyboard.getInlineKeyboardMarkup()).build());
        });
        log.info("执行botTask：{}", botJobId);
    }
    
}
